﻿using System;
using System.Data;

namespace GeneratorLibrary.Client.Base {
    public abstract class ClassColumnBase {
        #region members
        protected string memberName, propertyName, columnName, dataType;
        protected string member, property, dacAttribute, extraMethod = "";
        protected bool isIdentity, isPrimaryKey, isNullable, isForeignKey, isUniqueConstraint;
        protected string nullValue = "null";
        protected bool allowsIDataModel = false;

        protected int length, columnOrder, numericScale, numericPrecision, charMaxLength;
        #endregion
        #region properties
        public string DataType {
            get { return dataType; }
        }
        public bool IsUniqueConstraint {
            get { return isUniqueConstraint; }
        }
        public bool IsNullable {
            get { return isNullable; }
        }
        public string NullValue {
            get { return nullValue; }
        }
        public string ColumnName {
            get { return columnName; }
        }
        public string DacAttribute {
            get { return dacAttribute; }
        }
        public string Property {
            get { return property; }
        }
        public string ExtraMethod {
            get { return extraMethod; }
        }
        public string Member {
            get { return member; }
        }
        public bool IsPrimaryKey {
            get { return isPrimaryKey; }
        }
        public bool IsForeignKey {
            get { return isForeignKey; }
        }

        public bool AllowsIDataModel {
            get { return allowsIDataModel; }
        }
        public int CharMaxLength {
            get { return charMaxLength; }
        }
        public int NumericPrecision {
            get { return numericPrecision; }
        }
        public int NumericScale {
            get { return numericScale; }
        }
        public int ColumnOrder {
            get { return columnOrder; }
        }
        public int Length {
            get { return length; }
        }
        #endregion

        private ClassColumnBase() { }

        public ClassColumnBase(DataRow dr) {
            isIdentity = (Convert.ToInt32(dr["IsIdentity"]) == 1);
            isPrimaryKey = (Convert.ToInt32(dr["IsPrimaryKey"]) == 1);
            isNullable = (Convert.ToInt32(dr["IsNullable"]) == 1);
            isForeignKey = (Convert.ToInt32(dr["IsForeignKey"]) == 1);
            isUniqueConstraint = (Convert.ToInt32(dr["IsUniqueConstraint"]) == 1);

            //integer values
            length = dr.IsNull("Length") ? 0 : Convert.ToInt32(dr["Length"]);
            columnOrder = Convert.ToInt32(dr["columnOrder"]);
            numericScale = dr.IsNull("NumericScale") ? 0 : Convert.ToInt32(dr["NumericScale"]);
            numericPrecision = dr.IsNull("NumericPrecision") ? 0 : Convert.ToInt32(dr["NumericPrecision"]);
            charMaxLength = dr.IsNull("CharMaxLength") ? 0 : Convert.ToInt32(dr["CharMaxLength"]);

            columnName = dr["ColumnName"].ToString();

            string temp = dr["DataType"].ToString();
            dataType = getDotNetDataType(ref temp);
            setDbDataType(temp);

            generate();
        }
        public abstract string GetDotNetDefaultCoalesce();
        protected abstract void setDbDataType(string DbDataType);
        protected abstract string getDotNetDataType(ref string DbDataType);
        protected abstract void generate();
    }
}
